<?php

function db_connect() {
    $connection = mysqli_connect('localhost', 'root', '', 'aleksiev');
    if(!$connection) {
        echo 'No connection to the Database !';
        exit;
    }

    mysqli_set_charset($connection, 'utf8');
    return $connection;
}

function query($sql) {
    global $db_connection;
    return mysqli_query($db_connection, $sql);
}

function fetch($data) {
    return mysqli_fetch_assoc($data);
}

function fetchAll($data) {
    while($rs = mysqli_fetch_assoc($data)) {
        $result[] = $rs;
    }
    return $result;
}

function escape($str) {
    global $db_connection;
    return htmlspecialchars(mysqli_real_escape_string($db_connection, $str));
}

function esc($str) {
    global $db_connection;
    return mysqli_real_escape_string($db_connection, $str);
}

function htmlescape($str) {
    return htmlspecialchars($str);
}

function redirect($location, $uri = null) {
    
    if(strpos($location, '.php')) {
        $redirect = $location;
    } else {
        $redirect = $location.'.php';
    }
    
    if($uri) {
        $redirect .= '?'.$uri;
    }
    header('Location: '.$redirect);
    exit;
}

function loadSmarty($action, $data) {
    include 'plugins/Smarty/libs/Smarty.class.php';
    $smarty = new Smarty();

//    $smarty->debugging = true;

    $smarty->setTemplateDir('templates/default');
    $smarty->setCompileDir('plugins/Smarty/templates_c/');
    $smarty->setConfigDir('plugins/Smarty/configs/');
    $smarty->setCacheDir('plugins/Smarty/cache/');

    $smarty->assign($data);

    if(is_array($action)) {
        foreach($action as $act) {
            $smarty->display($act.'.tpl', $data);
        }
    } else {
        $smarty->display($dir.$action.'.tpl', $data);
    }
}

function getRefPage() {
    $refPage = explode('/', $_SERVER['HTTP_REFERER']);
    if($refPage[count($refPage) - 1]) {
        return $refPage[count($refPage) - 1];
    } else {
        return 'index';
    }
}

function getIpAddress() {
    if(trim($_SERVER['HTTP_CLIENT_IP']) != "") {   //Check IP
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } else if(trim($_SERVER['HTTP_X_FORWARDED_FOR']) != "") {   //Check for proxy
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

function getTags() {
    $posts = fetchAll(query('SELECT * FROM posts'));
    
    $tags = array();
    foreach($posts as $p) {
        $tt = explode(',', $p['tags']);
        foreach($tt as $t) {
            if(array_key_exists($t, $tags)) {
                $tags[$t]++;
            } else {
                $tags[$t] = 1;
            }
            $sum++;
        }
    }
    
    foreach($tags as $tag => $val) {
        $perc = ($val / $sum) * 100;
        if($val <= 3 || $perc <= 25) {
            $tags[$tag] = 'small';
        } else if($val > 3 && $perc > 25 && $perc <= 50) {
            $tags[$tag] = 'normal';
        } else if($val > 3 && $perc > 50 && $perc <= 75) {
            $tags[$tag] = 'medium';
        } else if($val > 3 && $perc > 75 && $perc <= 100) {
            $tags[$tag] = 'large';
        }
    }
    
    return $tags;
}